<?php
namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;
use App\Models\InboxDialog;
use App\Models\Inbox;
use Illuminate\Support\Facades\DB;

class MessageController extends MasterController
{
    
    public function __construct() 
    {
        parent::__construct();        
    }

    
    /**
     * 消息详情
     */
    public function show(Request $request)
    {
        $rid = $request->input('ruid',0);
        $result = InboxDialog::with('sender')->with('recipient')
                  ->where(array('recipient_uid'=>$rid,'sender_uid'=>$this->user->id))->orWhere(array('sender_uid'=>$rid,'recipient_uid'=>$this->user->id))
                  ->first();
        if(!empty($result))
        {
             $data = Inbox::where(array('dialog_id'=>$result->id,'user_id'=>$this->user->id,'semder_remove'=>0))
                        ->orWhere(array('dialog_id'=>$result->id,'receipt'=>$this->user->id,'recipient_remove'=>0))
                        ->orderBy('created_at','asc')->get();
             if (empty($data))
             {
                  return $this->formatJson('error','没有更多数据！');
             }
             if($result->sender_uid != $this->user->id)
             {
                  $result->sender_unread = 0;
                  $result->save();
                  $result->otherLogo = userlogo($result->sender_uid, 'small');
                  $result->rid = $result->sender_uid;
                  $result->recipientname = getUserName($result->sender_uid);
             }else{
                  $result->recipient_unread = 0;
                  $result->save();
                  $result->otherLogo = userlogo($result->recipient,'small');
                  $result->rid = $result->recipient_uid;
                  $result->recipientname = getUserName($result->recipient_uid);
             }
                 $result->selfLogo = userlogo($this->user->id,'small');
                 $result->uid = $this->user->id;
                 $result->data = $data;
                 return $this->formatJson('success','成功',$result);
        }else{
            $result = array();
            $result['rid'] = $rid;
            $result['recipientname'] =getUserName($rid);
            return $this->formatJson('falses','成功',$result);
        }

    }
    
    /**
     * 用户发送私信
     */
    public function store(\Illuminate\Http\Request $req)
    {
        if($req->ajax())
        {
            $recipient_uid = $req->input('rid',0);
            $notifyid = $req->input('id');
            $content = $req->input('content','');
            if($notifyid != 0)
            {
                $notify = InboxDialog::where(array('id'=>$notifyid))->first();
                if(empty($notify))
                {
                    return $this->formatJson('error','数据异常！');
                }
                elseif($notify->sender_uid != $this->user->id && $notify->recipient_uid != $this->user->id)
                {
                    return $this->formatJson('error','数据异常！');
                }
                elseif ($notify->sender_uid != $recipient_uid && $notify->recipient_uid != $recipient_uid)
                {
                    return $this->formatJson('error','数据异常！');
                }
                if($notify->sender_uid == $this->user->id)
                {
                    $notify->incrementAdd('sender_unread',1);
                }
                else
                {
                    $notify->incrementAdd('recipient_unread',1);
                }
                if(!empty($content))
                {
                    $notifyData = new Inbox();
                    $notifyData->message = $content;
                    $notifyData->dialog_id = $notifyid;
                    $notifyData->user_id = $this->user->id;
                    $notifyData->receipt = $recipient_uid;
                    $notifyData->save();
                    $notifyData->selfLogo = userlogo($this->user->id,'small');
                    $notifyData->created_at = date('Y-m-d H:i:s');
                    $notifyData->notifyid = $notifyid;
                    return $this->formatJson('success','成功！',$notifyData);
                }
                else
                {
                    return $this->formatJson('error','内容不能为空！');
                }
            }else{
               DB::beginTransaction();
               try {
                 if(!empty($content))
                 {
                   $notify = new InboxDialog();
                   $notify->sender_uid = $this->user->id;
                   $notify->recipient_uid = $recipient_uid;
                   $notify->save();
                   $notifyData = new Inbox();
                   $notifyData->message = $content;
                   $notifyData->dialog_id = $notify->id;
                   $notifyData->user_id = $this->user->id;
                   $notifyData->receipt = $recipient_uid;
                   $notifyData->save();
                   $notifyData->selfLogo = userlogo($this->user->id,'small');
                   $notifyData->created_at = date('Y-m-d H:i:s');
                   $notifyData->notifyid = $notify->id;
                 }else{
                   return $this->formatJson('error','内容不能为空！');
                 }
               }catch (\PDOException $e){
                   DB::rollback();
                   return $this->formatJson('falied','失败！');
               }
               DB::commit();
               return $this->formatJson('success','成功！',$notifyData);

            }
        }
        else 
        {
            abort(404);
        }
    }
    
}